package demo.socket;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;

public class ClientSock {
	public static void main(String[] args) {
		Socket sk = null;
		try {
			sk = new Socket("localhost", 1234);
			
			
			OutputStream os = sk.getOutputStream();
			String data = "asd";
			int leg = data.length();
			byte[] arrLeg = ByteBuffer.allocate(4).putInt(leg).array();
			os.write(arrLeg);
			os.write(data.getBytes());
			
			InputStream is = sk.getInputStream();
			arrLeg = new byte[4];
			is.read(arrLeg);
			leg = ByteBuffer.wrap(arrLeg).getInt();
			byte[] bt = new byte[leg];
			is.read(bt);
			System.out.println(new String(bt));
			os.close();
			is.close();
			sk.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
